/**
 * Created by Administrator on 2015/12/1.
 */
var mongodb = require('mongodb');
var ObjectID = mongodb.ObjectID;
var MongoClient = mongodb.MongoClient;
var assert = require('assert');
var Q = require('q');
var url = 'mongodb://localhost:27017/mydb';



//插入文章
/**
 * article structure
 * {type:1,
 *  title:xx
 *  userId:xx
 *  body:xx
 *  date:xx
 *  }
 *  type:1
 * @param article
 * @param callback
 */
function add_article(article,callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        article.type = 1;
        article.userId = ObjectID.createFromHexString(article.userId);
        return blog.insertOne(article);
    }).then(function(r){
//        console.log(r)
        callback(r);
    }).finally(function(){
        if(conn.close()){
            conn.close();
        }
    })
}

function edit_article(article,callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.updateOne({_id:ObjectID.createFromHexString(article._id)},{$set:{
            title:article.title,
            body:article.body
        }
        });
    }).then(function(r){
//        console.log(r)
        callback(r);
    }).finally(function(){
        conn.close();
    })
}

function delete_article(_id,callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.deleteOne({_id:ObjectID.createFromHexString(_id)});
    }).then(function(r){
        callback(r);
    }).finally(function(){
        conn.close();
    })
}

function get_article(_id,callback){
    if(typeof _id === 'string'){
        _id = ObjectID.createFromHexString(_id);
    }
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.find({_id:_id}).toArray();
    }).then(function(docs){
        if(docs.length > 0){
            var doc = docs[0];
            doc.id = doc._id.toHexString();
            doc.userId = doc.userId.toHexString();
            callback(doc);
        }else{
            callback({});
        }
    }).finally(function(){
        conn.close();
    })
}
function get_articlelist(callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.find({type:1}).toArray();
    }).then(function(docs){
        docs.forEach(function(doc){
            doc._id = doc._id.toHexString();
            doc.userId = doc.userId.toHexString();
        });
        callback(docs);
    }).finally(function(){
        conn.close();
    })
}
/********************user operation******************************/
/**
 * {
 *  _id:
 *  username:
 *  nickname:
 *  password:
 * }
 * type:3
 * @param user
 * @param callback
 */
function add_user(user,callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        user.type = 3;
        return blog.insertOne(user);
    }).then(function(r){
//        console.log(r)
        callback(r);
    }).finally(function(){
        conn.close();
    })
}

function edit_user(user,callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.updateOne({_id:ObjectID.createFromHexString(user._id)},
            {$set:{
            username:user.username,
            nickname:user.nickname
            }
        });
    }).then(function(r){
        callback(r);
    }).finally(function(){
        conn.close();
    })
}

function delete_user(_id,callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.deleteOne({_id:ObjectID.createFromHexString(_id)});
    }).then(function(r){
        callback(r);
    }).finally(function(){
        conn.close();
    })
}

function get_user(_id,callback){
    if(typeof id === 'string'){
        _id = ObjectID.createFromHexString(_id);
    }
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.find({_id:_id}).toArray();
    }).then(function(docs){
        if(docs.length > 0){
            var doc = docs[0];
            doc._id = doc._id.toHexString();
            callback(doc);
        }else{
            callback({});
        }
    }).finally(function(){
        conn.close();
    })
}

function get_userlist(callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.find({type:3}).toArray();
    }).then(function(users){
        users.forEach(function(user){
            user._id = user._id.toHexString();
        });
        callback(users);
    }).finally(function(){
        conn.close();
    })
}
//get_userlist(console.dir);
/************************comment operation****************************/
function get_comment(_id,callback){
    if(typeof _id === 'string'){
        _id = ObjectID.createFromHexString(_id);
    }
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.find({_id:_id}).toArray();
    }).then(function(comments){
        if(comments.length>0){
            var comment = comments[0];
            comment._id = comment._id.toHexString();
            comment.articleId = comment.article.toHexString();
            callback(doc);
        }else{
            callback({});
        }
    }).finally(function(){
        conn.close();
    })
}

function edit_comment(comment,callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.updateOne({_id:ObjectID.createFromHexString(comment._id)},
            {$set:{
                content:comment.content,
                date:Date.now()
            }
            });
    }).then(function(r){
        callback(r);
    }).finally(function(){
        conn.close();
    })
}

function add_comment(comment,callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        comment.type = 2;
        return blog.insertOne(user);
    }).then(function(r){
        callback(r);
    }).finally(function(){
        conn.close();
    })
}

function delete_comment(_id,callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.deleteOne({_id:ObjectID.createFromHexString(_id)});
    }).then(function(r){
        callback(r);
    }).finally(function(){
        conn.close();
    })
}

function get_commentlist(callback){
    var p = MongoClient.connect(url,{promiseLibrary: Q.promise});
    var conn = undefined;
    p.then(function(db){
        var blog = db.collection('blog');
        conn = db;
        return blog.find({type:2}).toArray();
    }).then(function(comments){
        comments.forEach(function(comment){
            comment._id = comment._id.toHexString();
            comment.articleId = comment.articleId.toHexString();
        });
        callback(comments);
    }).finally(function(){
        conn.close();
    })
}


+function(){
    function getArticle(_id,callback){
        if(typeof _id === 'string'){
            _id = ObjectID.createFromHexString(_id);
        }
        var test = 'mongodb://localhost:27017/test';
        var p = MongoClient.connect(test,{promiseLibrary: Q.promise});
        var conn = undefined;
        p.then(function(db){
            var blog = db.collection('articles');
            conn = db;
            return blog.find({userId:_id}).toArray();
        }).then(function(docs){
            if(docs.length > 0){
                var doc = docs[0];
                doc.id = doc._id.toHexString();
                doc.userId = doc.userId.toHexString();
                callback(doc);
            }else{
                callback({});
            }
        }).finally(function(){
            conn.close();
        })
    }
    getArticle('569528daa47d315817a2b5db',console.log);
}();